class Food {
    constructor(gridSize) {
        this.gridSize = gridSize;
        this.position = this.generatePosition();
    }

    generatePosition(snakeSegments = []) {
        let newPosition;
        let isPositionValid = false;

        while (!isPositionValid) {
            newPosition = {
                x: Math.floor(Math.random() * this.gridSize),
                y: Math.floor(Math.random() * this.gridSize)
            };

            // 检查是否与蛇身重叠
            isPositionValid = true;
            for (const segment of snakeSegments) {
                if (segment.x === newPosition.x && segment.y === newPosition.y) {
                    isPositionValid = false;
                    break;
                }
            }
        }

        return newPosition;
    }

    reset(snakeSegments) {
        this.position = this.generatePosition(snakeSegments);
    }
}